#include "hplcpumppanel.h"
#include "modulemanager.h"
#include "modulehplcpump.h"
#include "modulehplcpumpparamiddefine.h"
#include "modulehplccmdid.h"
#include <QDebug>
#include <QCloseEvent>
#include "mainwindow.h"

HplcPumpPanel::HplcPumpPanel(QWidget *parent) : QWidget(parent)
{
    setWindowTitle(tr("Hplc Pump Panel"));
    setAttribute(Qt::WA_DeleteOnClose);

    ModuleHplcPump *mod = static_cast<ModuleHplcPump*>(moduleManager->find(ModuleID_HPLC_PumpA));
    if (mod == nullptr)
        return;
    qDebug() << mod->getName();
    connect(mod, &ModuleHplcPump::parseCompleted,
        this, &HplcPumpPanel::onParseCompleted, Qt::QueuedConnection);

    // 流速
    m_lbFlow = new QLabel(tr("Flow(ml/min)"));
    m_leFlow = new QLineEdit();
    m_pbFlowSend = new QPushButton(tr("Send"));
    connect(m_pbFlowSend, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leFlow->text().toInt());
        mod->setProtocolCmd(hplc::PumpCmdID_SetFlow, var);
    });
    m_hblFlow = new QHBoxLayout();
    m_hblFlow->addWidget(m_lbFlow);
    m_hblFlow->addWidget(m_leFlow);
    m_hblFlow->addWidget(m_pbFlowSend);

    // 最大压力
    m_lbPressureMax = new QLabel(tr("PressureMax(MPa)"));
    m_lePressureMax = new QLineEdit();
    m_pbPressureMaxSend = new QPushButton(tr("Send"));
    connect(m_pbPressureMaxSend, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_lePressureMax->text().toInt());
        mod->setProtocolCmd(hplc::PumpCmdID_SetPressureMax, var);
    });
    m_hblPressureMax = new QHBoxLayout();
    m_hblPressureMax->addWidget(m_lbPressureMax);
    m_hblPressureMax->addWidget(m_lePressureMax);
    m_hblPressureMax->addWidget(m_pbPressureMaxSend);

    // 最小压力
    m_lbPressureMin = new QLabel(tr("PressureMin(MPa)"));
    m_lePressureMin = new QLineEdit();
    m_pbPressureMinSend = new QPushButton(tr("Send"));
    connect(m_pbPressureMinSend, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_lePressureMin->text().toInt());
        mod->setProtocolCmd(hplc::PumpCmdID_SetPressureMin, var);
    });
    m_hblPressureMin = new QHBoxLayout();
    m_hblPressureMin->addWidget(m_lbPressureMin);
    m_hblPressureMin->addWidget(m_lePressureMin);
    m_hblPressureMin->addWidget(m_pbPressureMinSend);

    // 启动
    m_pbStart = new QPushButton(tr("Start"));
    connect(m_pbStart, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(true);
        mod->setProtocolCmd(hplc::PumpCmdID_SetStartStop, var);
    });
    m_pbStop = new QPushButton(tr("Stop"));
    connect(m_pbStop, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(false);
        mod->setProtocolCmd(hplc::PumpCmdID_SetStartStop, var);
    });
    m_hblStart = new QHBoxLayout();
    m_hblStart->addWidget(m_pbStart);
    m_hblStart->addWidget(m_pbStop);

    // 当前压力
    m_lbCurrentPressure = new QLabel(tr("0.00 MPa"));
    // 压力归零
    m_pbPressureClear = new QPushButton(tr("PressureClear"));
    connect(m_pbPressureClear, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->setProtocolCmd(hplc::PumpCmdID_SetPressZero, var);
    });
    // 压力曲线
    m_pbPressureCurve = new QPushButton(tr("PressureCurve"));
    connect(m_pbPressureCurve, &QPushButton::clicked, this, [=](){
        if (m_pressureCurve == nullptr)
        {
            m_pressureCurve = new Curve();
            m_pressureCurve->setTitle("Pressure Curve");
            m_pressureCurve->setXAxisName(tr("time(x50ms)"));
            m_pressureCurve->addGraph(tr("pressure"), QColor(Qt::red));
//            m_pressureCurve->setGeometry(1000, 200, 800, 600);
//            m_pressureCurve->show();

            QMdiSubWindow *subWin = mainwindow->addSubWindow(m_pressureCurve);
            subWin->resize(mainwindow->size() * 2 / 3);
            m_pressureCurve->show();

            connect(subWin, &QWidget::destroyed, [=](){
                m_pressureCurve = nullptr;
            });
        }
        else
        {
            if (m_pressureCurve->isHidden())
                m_pressureCurve->show();
            else
                m_pressureCurve->hide();
        }
    });
    m_hblCurrentPressure = new QHBoxLayout();
    m_hblCurrentPressure->addWidget(m_lbCurrentPressure);
    m_hblCurrentPressure->addWidget(m_pbPressureClear);
    m_hblCurrentPressure->addWidget(m_pbPressureCurve);

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(m_hblFlow);
    vbl->addLayout(m_hblPressureMax);
    vbl->addLayout(m_hblPressureMin);
    vbl->addLayout(m_hblStart);
    vbl->addLayout(m_hblCurrentPressure);
    setLayout(vbl);
}

HplcPumpPanel::~HplcPumpPanel()
{
    if (m_pressureCurve)
        m_pressureCurve->deleteLater();
}

void HplcPumpPanel::closeEvent(QCloseEvent *event)
{
    disconnect();
    event->accept();
}

void HplcPumpPanel::onParseCompleted(int id, const QVariant &var)
{
    switch (id)
    {
        case hplc::PumpCmdID_SetPressure:
        {
            float pressure = var.toFloat();
            m_lbCurrentPressure->setText(QString::number(pressure, 'g', 2) + " MPa");

            if (m_pressureCurve)
            {
                m_pressureCurve->onAddGraphData(1, pressure);
            }
        }
        break;
        default:
        break;
    }
}
